// pages/detail/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guid:'',
    imgs: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],//星星评价
    imgSrc:'../../images/start1.png',
    imgSrc1:'../../images/start2.png',
    BigImgUrl:'',//评论大图地址
    bigFlag:false,//是否显示评论大图
    tabList:['商品','详情','评价'],
    indx:0,//tab切换
    imgUrlList:[],//商品详情滚动图片地址
    imgDefaultUrl:'',//默认图片
    videoUrlList:[],
    height:'',
    imgTag:true,//banner图与视频切换显示
    videoHeight:'',
    goodsInfo:'',
    specs:[],//规格信息
    goodName:'',
    time:'',
    h:'',
    m:'',
    s:'',
    ms:9,
    swichTag:true,//是否显示图片与视频切换按钮
    productDetail:'',//商品详情
    commentnums:'0',//评论数量
    homeInfo:[],//首页评价
    allInfo:[],//所有评论
    currentPage:1,
    cartFlag:false,//是否显示加入购物车页面
    addNum:[]//加入购物车数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    console.log("options",options.guid)
    this.setData({
      guid:options.guid
    })
    //获取首页商品评价
    wx.request({
      url: app.globalData.url +'/homepage/product/comments',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        productId: _this.data.guid,
        currentPageForApp: _this.data.currentPage,
        showCount:10
      },
      success(res){
        console.log('首页评论数量',res)
        // console.log('评论图片', res.data.comments[0].images)
        let arr = []
        let arr1 = res.data.comments
        if(arr1){
          arr1.forEach(function (value, index) {
            if (value.images){
              arr.push(JSON.parse(value.images))
            }else{
              arr.push('')
            }
          })
          setTimeout(function () {
            console.log('arrList',arr)
            arr1.forEach(function (value, index) {
              value.imageUrl2 = arr[index]
            })
            let arr2 = []
            arr2.push(arr1[0])
            _this.setData({
              allInfo: arr1,
              homeInfo: arr2
            })
            console.log("arr", _this.data.homeInfo)
          }, 100)
        }
        _this.setData({
          commentnums: res.data.commentnums
        })
        
      }
    })
    //获取商品详情
    wx.request({
      url: app.globalData.url +'/homepage/product/detail',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        productId:_this.data.guid
      },
      success(res) {
        console.log("商品详情",res)
        let Ospecs = JSON.parse(res.data.detail.specs)
        Ospecs.forEach(function(value,index) {
          value.num = 0
        })
        _this.setData({
          goodsInfo:res.data.detail,
          specs: Ospecs,
          goodName: res.data.detail.productName,
          time: res.data.detail.createTime,
          productDetail: res.data.detail.productDetail
        })
        console.log('规格', _this.data.specs);
        //规格倒计时
        let startTime = _this.data.time
        let date = new Date(startTime)
        console.log(date)
        let h = date.getHours();
        let m = date.getMinutes();
        let s = date.getSeconds();
        _this.setData({
          h,
          m,
          s
        })

        //自定义倒计时
        let t = setInterval(function() {

          _this.setData({
            ms:_this.data.ms-1
          })
          if(_this.data.ms <=0&&_this.data.s>0){
            _this.setData({
              ms:9,
              s:_this.data.s-1
            })
          }
          if(_this.data.s<=0&&_this.data.m>0) {
            _this.setData({
              s:59,
              m:_this.data.m-1
            })
          }
          if(_this.data.m<=0&&_this.data.h>0) {
            _this.setData({
              m:59,
              h:_this.data.h-1
            })
          }
          if(_this.data.h == 0 && _this.data.m == 0 && _this.data.s ==0){
            clearInterval(t)
          }
        },100)

        const list = JSON.parse(res.data.detail.images)
        if (res.data.detail.videos){
          const videoList = JSON.parse(res.data.detail.videos)
          _this.setData({
            videoUrlList: videoList,
            swichTag:true
          })
        }else{
          _this.setData({
            swichTag:false
          })
        }
        
        console.log('list',list)
        _this.setData({
          imgUrlList:list
        })
        list.forEach(function(value,index) {
          if (value.isDefalut == 1){
            _this.setData({
              imgDefaultUrl: value.imageUrl
            })
          }
        })
        console.log('默认图片', _this.data.imgDefaultUrl)
      }
    })
    wx.getSystemInfo({
      success: function(res) {
        console.log('系统信息',res)
        let windowWidth = res.windowWidth
        let videoHeight = (225/300)*windowWidth
        _this.setData({
          videoHeight: videoHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(111)
    console.log(this.route)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const _this = this
    let flag =1
    if(_this.data.indx == 2){
      if(flag ==1){
        flag = 0;//防止多次刷新
        _this.setData({
          currentPage: _this.data.currentPage+1
        })
        wx.request({
          url: app.globalData.url + '/homepage/product/comments',
          method: 'post',
          header: {
            "content-type": 'application/x-www-form-urlencoded',
            "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
            "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
          },
          data: {
            productId: _this.data.guid,
            currentPageForApp: _this.data.currentPage,
            showCount: 10
          },
          success(res) {
            console.log('下拉加载数据完成',res)
            let arra = []
            let arrb =[]
            let arrz = _this.data.allInfo
            arra = res.data.comments
            arra.forEach(function(value,index) {
              if (value.images){
                arrb.push(JSON.parse(value.images))
              }else{
                arrb.push('')
              }
              value.imageUrl2 = arrb[index]
              arrz.push(value)   
            })
            _this.setData({
              allInfo:arrz
            })
            console.log('追加数据后的arrz', arrz);
            flag = 1;
          }
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      path: '/pages/detail/detail?guid='+this.data.guid
    }
  },
  switch(res){
    console.log('切换',res)
    this.setData({
      indx:res.currentTarget.dataset.num
    })
    console.log(this.data.indx)
  },
  imgLoad(e) {
    //图片真实高度宽度
    let Oheight = e.detail.height
    let Owidth = e.detail.width
    //屏幕宽度
    let Owidth2 = wx.getSystemInfoSync().windowWidth
    //图片高度
    let Oheight2 = (Owidth2*Oheight)/Owidth
    this.setData({
      height:Oheight2
    }) 
  },
  banner_pic(e) {
    this.setData({
      imgTag:true
    })
  },
  banner_video(e) {
    this.setData({
      imgTag: false
    })
  },
  seeAll(res) {
    this.setData({
      indx: 2
    })
  },
  fasdCart() {
    this.setData({
      cartFlag: !this.data.cartFlag
    })
  },
  fasdCart2() {
    cartFlag:true
  },
  add(e) {
    const _this = this
    let Oin = e.currentTarget.dataset.index;
    let arrC = this.data.specs
    if (arrC[Oin].num < arrC[Oin].inventoryNum){
      arrC[Oin].num++
    }
    
    this.setData({
      specs: arrC
    })
    console.log('数量', arrC[Oin].num)
    wx.request({
      url: app.globalData.url + '/shop/car/modify',
      header: {
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data: {
        productId: _this.data.guid,
        productSpecsId: arrC[Oin].specsId,
        carNum: 1,
        operator: 'add'
      },
      method: 'post',
      success(res) {
        console.log('增加购物车数量', res)
      }
    })
  },
  less(e) {
    const _this = this
    let Oin = e.currentTarget.dataset.index;
    let arrC = this.data.specs
    if (arrC[Oin].num>0){
      arrC[Oin].num--
    }
    this.setData({
      specs: arrC
    })
    wx.request({
      url: app.globalData.url + '/shop/car/modify',
      data: {
        productId: _this.data.guid,
        productSpecsId: arrC[Oin].specsId,
        carNum: 1,
        operator:'del'
      },
      header: {
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      method: 'post',
      success(res) {
        console.log('减少购物车数量',res)
      }
    })
  },
  edit() {
    this.setData({
      cartFlag: !this.data.cartFlag
    })
  },
  //点击显示评论大图
  bigImgShow(e) {
    this.setData({
      bigFlag:true,
      BigImgUrl: e.currentTarget.dataset.url
    })
  },
  //隐藏评论大图
  bigImgHide() {
    this.setData({
      bigFlag:false
    })
  }
})